package com.xpm.adapter.simple;


import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xpm.R;
import com.xpm.entity.ItemBean;

import java.util.List;

public class ListViewAdapter extends RecycleViewBaseAdapter {

    private static final int SIMPLE_VIEW_TYPE = 0;
    private static final int LOAD_MORE_VIEW_TYPE = 1;
    private OnRefreshListener mUpPullRefreshListener;

    public ListViewAdapter(List<ItemBean> datas) {
        super(datas);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(LOAD_MORE_VIEW_TYPE == viewType){
            return new LoadMoreViewHolder(View.inflate(parent.getContext(), R.layout.item_list_loader_more, null));
        }
        return super.onCreateViewHolder(parent, viewType);
    }
    @Override
    protected int getResourceId() {
        return R.layout.item_list_view;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==LOAD_MORE_VIEW_TYPE){
            ((LoadMoreViewHolder) holder).update(LoadMoreViewHolder.LOADER_STATE_LOADING);
            return ;
        }
        super.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemViewType(int position) {
        if(getItemCount()-1==position){
            // 最后一个，则返回加载更多
            return LOAD_MORE_VIEW_TYPE;
        }
        return SIMPLE_VIEW_TYPE;
    }

    /**
     * 设置刷的监听的接口
     */
    public void setOnRefreshListener(OnRefreshListener listener) {
        this.mUpPullRefreshListener = listener;
    }

    //定义接口
    public interface OnRefreshListener {
        void onUpPullRefresh(LoadMoreViewHolder loaderMoreHolder);
    }

    public class LoadMoreViewHolder extends RecyclerView.ViewHolder{
        public static final int LOADER_STATE_LOADING = 0;
        public static final int LOADER_STATE_RELOAD = 1;
        public static final int LOADER_STATE_NORMAL = 2;


        private LinearLayout mLading;
        private TextView mReLoad;

        public LoadMoreViewHolder(@NonNull View itemView) {
            super(itemView);
            mLading = (LinearLayout) itemView.findViewById(R.id.loading);
            mReLoad = (TextView) itemView.findViewById(R.id.reload);

            mReLoad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //这里面要去触发加载数据
                    update(LOADER_STATE_LOADING);
                }
            });

        }
        public void update(int state) {

            //重置控件的状态
            mLading.setVisibility(View.GONE);
            mReLoad.setVisibility(View.GONE);

            switch (state) {
                case LOADER_STATE_LOADING:
                    mLading.setVisibility(View.VISIBLE);
                    //触发加载数据
                    startLoaderMore();
                    break;

                case LOADER_STATE_RELOAD:
                    mReLoad.setVisibility(View.VISIBLE);
                    break;

                case LOADER_STATE_NORMAL:
                    mLading.setVisibility(View.GONE);
                    mReLoad.setVisibility(View.GONE);
                    break;
            }
        }

        private void startLoaderMore() {
            if (mUpPullRefreshListener != null) {
                mUpPullRefreshListener.onUpPullRefresh(this);
            }
        }
    }
}

/**
 * 基础RecyclerView使用
 */
//public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.InnerHolder> {
//
//    /** 设置数据 **/
//     public List<ItemBean> mData;
//
//    /**
//     * 构造函数
//     * @param data
//     */
//    public ListViewAdapter(List<ItemBean> data) {
//        this.mData = data;
//    }
//
//    /**
//     * 用于创建条目的View
//     * @return ListViewAdapter.InnerHolder
//     */
//    @NonNull
//    @Override
//    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = View.inflate(parent.getContext(), R.layout.item_list_view, null);
//        return new InnerHolder(view);
//    }
//
//    /**
//     * 用于绑定条目的数据
//     */
//    @Override
//    public void onBindViewHolder(@NonNull ListViewAdapter.InnerHolder holder, int position) {
//        ItemBean bean = mData.get(position);
//        holder.setData(bean);
//    }
//
//    /**
//     * 返回条目个数
//     * @return
//     */
//    @Override
//    public int getItemCount() {
//        return mData == null ? 0 : mData.size();
//    }
//
//    public static class InnerHolder extends RecyclerView.ViewHolder{
//        ItemBean itemBean;
//        ImageView iv_icon;
//        TextView tv_title;
//
//        public InnerHolder(@NonNull View itemView) {
//            super(itemView);
//            // 找到ItemView
//
//            iv_icon = itemView.findViewById(R.id.iv_icon);
//            tv_title = itemView.findViewById(R.id.tv_title);
//
//        }
//
//        /**
//         * 用于设置数据
//         * @param itemBean
//         */
//        public void setData(ItemBean itemBean) {
//            this.itemBean =itemBean;
//            iv_icon.setImageResource(itemBean.icon);
//            tv_title.setText(itemBean.title);
//        }
//    }
//}
